"""
RPS (Request Per Second) 测试
"""
import datetime
from locust import HttpUser, TaskSet, task


class WebsiteTasks(TaskSet):
    def on_start(self):
        url = '/user/token-pair/'
        form_data = {
            "username": "test01",
            "password": "test123456",
        }
        response = self.client.post(url, form_data)
        data = response.json()
        access_token = "Bearer " + data['access']
        self.client.headers = {"Authorization": access_token}

    def articles_list(self):
        self.client.get("/blog/articles/")

    def articles_create(self):
        current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        url = "/blog/articles/"
        form_data = {
            "title": f"标题 {current_time}",
            "content": f"内容{current_time}"
        }
        self.client.post(url, form_data)

    # 使用装饰器 @task(1)
    tasks = {
        articles_list: 2,
        # articles_create: 1,
    }


class WebsiteUser(HttpUser):
    tasks = [WebsiteTasks]
    host = "http://127.0.0.1:8888"
    min_wait = 1000
    max_wait = 5000
